package info.brenstar.boxedin.collisions;

import org.newdawn.slick.geom.Rectangle;

import info.brenstar.boxedin.core.*;

public class CollisionChecker {
	
	private CollisionChecker() {}
	
	public static CollisionMap Map;
	
	/**
	 * Gets a sprite that intersects Rectangle rect. If multiple sprites intersect the
	 * same rectangle, the sprite with the highest depth will be returned.
	 * @param rect 
	 * @return A sprite that intersects rect, if no sprite is found, null is returned
	 */
	public static Sprite getSpriteAt(Rectangle rect) {
		Sprite collide = null;
		int id = Map.getSectorId((int)rect.getX(), (int)rect.getY());
		
		Sector[] check = new Sector[4];
		check[0] = Map.getSector(id);
		
		boolean breakEarly = false;
		
		for (Sector sector : check) {
			if (breakEarly) { break; }
			if (sector != null) {
				for (Sprite sprite : sector.getSprites()) {
					if (sprite.getMask().intersects(rect)) {
						collide = sprite;
						breakEarly = true;
						break;
					}
				}
			}
		}
		
		return collide;
	}
	
	/**
	 * Checks the sprite for a collision. A collision occurs when the sprite's bounding box intersects
	 * another sprite's bounding box.
	 * @param sprite
	 * @return
	 */
	public static boolean isCollision(Sprite sprite) {
		return false;
	}
	
	/**
	 * Checks the sprite for a collision. If a collision is found, the colliding sprite will be returned
	 * @param sprite The sprite to check for a collision
	 * @return The colliding sprite, null if no collision was found
	 */
	public static Sprite getCollision(Sprite sprite) {
		return null;
	}
	
}
